import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:convert' as convert;

import 'cartoon_list_double.dart';

class cartoon_list_double_view extends StatefulWidget {
  cartoon_list_double_view({Key key, this.tage_id}) : super(key: key);
  int tage_id;
  @override
  State<cartoon_list_double_view> createState() =>
      _cartoon_list_double_viewState();
}

class _cartoon_list_double_viewState extends State<cartoon_list_double_view> {
  List cartoon_list1 = [
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
  ];

  List cartoon_list0 = [
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
    {
      'cartoon_id': 1,
      'cartoon_name': '间谍过家家',
      'cartoon_tage': 5,
      'cartoon_leader_image':
          'https://nianyiman-1309342317.cos-website.ap-guangzhou.myqcloud.com/jdgjj/jdgjj0.png'
    },
  ];
  ScrollController _scrollController = new ScrollController();
  Future _future;

  @override
  void initState() {
    // TODO: implement initState
    _future = _getinformation();
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        // _onLoadmore();
        print('滑到了最底部');
        _getinformation();
        // cartoon_list1..addAll(cartoon_list0);
        setState(() {});
      }
    });
  }

  int dou_page = 0;
  List car_listRes = [];
  _getinformation() async {
    dou_page++;
    print('异步调用');
    Response response;
    var dio = Dio();
    dio.options.responseType = ResponseType.plain;
    response = await dio.get('http://119.91.231.198:3033/getCarTage?tage_id=' +
        widget.tage_id.toString() +
        '');
    var res = convert.jsonDecode(response.toString());
    for (var i = 6 * (dou_page - 1); i < 6 * dou_page; i++) {
      // print(res['contentArr']);
      car_listRes.add(res['contentArr'][i]);
    }
    // print(res['contentArr'][0]);
    print(car_listRes);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _future,
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return RefreshIndicator(
            onRefresh: () async {
              //模拟网络请求
              setState(() {
                // rank_num = 0;
              });
              print('上拉刷新');
              await Future.delayed(Duration(milliseconds: 2000));
              //结束刷新
              return Future.value(true);
            },
            child: ListView(
              controller: _scrollController,
              // shrinkWrap: true,
              children: [
                Container(
                  margin: EdgeInsets.only(top: 20),
                  alignment: Alignment.center,
                  child: cartoon_list_double(
                    cartoon_list: car_listRes,
                  ),
                )
              ],
            ),
          );
        } else {
          return Container();
        }
      },
    );
  }
}
